package com.gitee.wsl.compose.ui.picker.color

/**
 * A representation of Color in Hue, Saturation and Value form.
 */
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.ui.graphics.Color
import com.github.ajalt.colormath.model.HSV
import com.github.ajalt.colormath.model.RGB

/**
 * A representation of Color in Hue, Saturation and Value form.
 */
data class HsvColor(

	// from = 0.0, to = 360.0
	val hue: Float,

	// from = 0.0, to = 1.0
	val saturation: Float,

	// from = 0.0, to = 1.0
	val value: Float,

	// from = 0.0, to = 1.0
	val alpha: Float
) {

	fun toColor(): Color {
		val hsv = HSV(hue, saturation, value, alpha)
		val rgb = hsv.toSRGB()
		return Color(rgb.redInt, rgb.greenInt, rgb.blueInt, rgb.alphaInt)
	}

	companion object {

		val DEFAULT = HsvColor(360f, 1.0f, 1.0f, 1.0f)

		/**
		 *  the color math hsv to local hsv color
		 */
		private fun HSV.toColor(): HsvColor {
			return HsvColor(
				hue = if (this.h.isNaN()) 0f else this.h,
				saturation = this.s,
				value = this.v,
				alpha = this.alpha
			)
		}

		fun from(color: Color): HsvColor {
			return RGB(
				color.red,
				color.green,
				color.blue,
				color.alpha,
			).toHSV().toColor()
		}

		val Saver: Saver<HsvColor, *> = listSaver(
			save = {
				listOf(
					it.hue,
					it.saturation,
					it.value,
					it.alpha
				)
			},
			restore = {
				HsvColor(
					it[0],
					it[1],
					it[2],
					it[3]
				)
			}
		)
	}
}
